package com.tianqu.tms.www.settle.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.TransferSettlementDate;
import com.tianqu.tms.entity.TransferSettlementDay;
import com.tianqu.tms.entity.UserAuth;
import com.tianqu.tms.www.settle.viewhelper.TransferSettleViewHelper;

public class TransferSettleSearchInputAction extends TmsAction {

	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		TransferSettleViewHelper view = new TransferSettleViewHelper();
		if((TransferSettleViewHelper)uc.getAttribute(TmsConstants.VIEWHELP)!=null)
		{
			view =(TransferSettleViewHelper)uc.getAttribute(TmsConstants.VIEWHELP);
		}
		//查询当前用户
		TmsUserProfile profile = uc.getUserProfile();
		UserAuth auth = new UserAuth();
		boolean flag = auth.getAuthInfoByRole(profile.getUniqueId(), 470);
		if(flag)
		{
			//当前用户为470
			view.setRoleFlag("1");
		}else{
			//不为470
			view.setRoleFlag("2");
		}
		if("1".equals(view.getSettleType()))
		{ 
			if(flag)
			{
				TransferSettlementDate settleDate =new TransferSettlementDate();
				settleDate = settleDate.getDateInfo(2,profile.getUniqueId());
				if(settleDate!=null)
				{
					//加入开始日期和结束日期
					view.setStartDate(settleDate.getStartDate());
					view.setEndDate(settleDate.getEndDate());
					view.setSettleId(String.valueOf(settleDate.getId()));
				}else{
					view.setShowBsFlag("1");
				}	
			}else{
				TransferSettlementDay day = new TransferSettlementDay();
				day = day.getTransferSettlementDayInfo();
				if(day!=null)
				{
					//页面显示yyyy-mm-dd
					view.setSettleDate(day.getSettleDate());
					view.setSettlePeriodId(String.valueOf(day.getId()));
				}else{
					view.setShowBsFlag("1");
				}
			}
		}

		request.setAttribute(TmsConstants.VIEWHELP, view);
		return mapping.findForward(TmsConstants.SUCCESS); 
	}

}
